<?php
session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
include_once( '../config.php' );
$code = $_GET["code"];
require_once './class/config.inc.php';
require_once './class/RenrenOAuthApiService.class.php';
require_once './class/RenrenRestApiService.class.php';

if($code)
	{
//获取accesstoken
$oauthApi = new RenrenOAuthApiService;
$post_params = array('client_id'=>$config->APIKey,
		'client_secret'=>$config->SecretKey,
		'redirect_uri'=>$config->redirecturi,
		'grant_type'=>'authorization_code',
		'code'=>$code
		);
$token_url='http://graph.renren.com/oauth/token';

$access_info=$oauthApi->rr_post_curl($token_url,$post_params);//使用code换取token
//$access_info=$oauthApi->rr_post_fopen($token_url,$post_params);//如果你的环境无法支持curl函数，可以用基于fopen函数的该函数发送请求
dump($access_info);
$access_token=$access_info["access_token"];
$expires_in=$access_info["expires_in"]*1000;
$refresh_token=$access_info["refresh_token"];
//session_start();
$_SESSION["access_token"]=$access_token;
//获取用户信息RenrenRestApiService
$restApi = new RenrenRestApiService;
$params = array('fields'=>'uid,name,sex,birthday,mainurl,hometown_location,university_history,tinyurl,headurl','access_token'=>$access_token);
$res = $restApi->rr_post_curl('users.getInfo', $params);//curl函数发送请求
$userId = $res[0]["uid"];
$username = $res[0]["name"];
echo $access_token;
/*
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME,$con);
mysql_query("SET NAMES utf8");
if($userId){
	$domain = getdomain1();
	$sql="select * from cai_uid where sns='renren' and uid='".trim($userId)."' and domain='".$domain."'"; 
	$result=mysql_query($sql);
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if (!mysql_num_rows($result)){
	    $sql2="INSERT INTO `cai_uid` (`id`, `uid`, `token` , `name`,`sns`,`expires`,`domain`) VALUES (NULL,'".trim($userId)."','".$_SESSION["access_token"]."','".$username."','renren','".$expires_in."','".$domain."');";
	    if(mysql_query($sql2)){
	        echo "插入成功";
	    }else{
	        echo "插入失败";
	    } 
	}else{
	    $sql3="UPDATE `cai_uid` SET `token` =  '".$_SESSION["access_token"]."' ,`name` =  '".$username."',`expires` =  '".$expires_in."' WHERE  uid='".$userId."' and sns= 'renren';";
	    if(mysql_query($sql3)){
	        echo "更新成功";
	    }else{
	        echo "更新失败";
	    } 
	}
}*/}

function getdomain1(){
    $host=$_SERVER['HTTP_HOST'];
    $host=strtolower($host);
    if(strpos($host,'/')!==false){
        $parse = @parse_url($host);
        $host = $parse['host'];
    }
    $topleveldomaindb=array('com','edu','gov','int','mil','net','org','biz','info','pro','name','museum','coop','aero','xxx','idv','mobi','cc','me');
    $str='';
    foreach($topleveldomaindb as $v){
        $str.=($str ? '|' : '').$v;
    }
    $matchstr="[^\.]+\.(?:(".$str.")|\w{2}|((".$str.")\.\w{2}))$";
    if(preg_match("/".$matchstr."/ies",$host,$matchs)){
        $domain=$matchs['0'];
    }else{
        $domain=$host;
    }
    return $domain;
}
?>
<a href="../shouquan.php" >查看所有授权</a>
<a href="main.php" >同步功能入口:人人API、人人分享、人人Dialog</a><br>

</body>
</html>